#include "Budget.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>

Budget::Budget(const std::string& filename) : budgetFile(filename) {
    loadFromFile();
}

bool Budget::setMonthlyBudget(const std::string& yearMonth, double amount) {
    if (amount <= 0) {
        std::cout << "错误：预算金额必须为正数！" << std::endl;
        return false;
    }

    // 验证年月格式 YYYY-MM
    if (yearMonth.length() != 7 || yearMonth[4] != '-') {
        std::cout << "错误：年月格式不正确！请使用 YYYY-MM 格式。" << std::endl;
        return false;
    }

    monthlyBudgets[yearMonth] = amount;
    std::cout << "月度预算设置成功：" << yearMonth << " = "
              << std::fixed << std::setprecision(2) << amount << std::endl;
    saveToFile();
    return true;
}

bool Budget::setCategoryBudget(const std::string& category, double amount) {
    if (amount <= 0) {
        std::cout << "错误：预算金额必须为正数！" << std::endl;
        return false;
    }

    if (category.empty()) {
        std::cout << "错误：类别名称不能为空！" << std::endl;
        return false;
    }

    categoryBudgets[category] = amount;
    std::cout << "类别预算设置成功：" << category << " = "
              << std::fixed << std::setprecision(2) << amount << std::endl;
    saveToFile();
    return true;
}

double Budget::getMonthlyBudget(const std::string& yearMonth) const {
    auto it = monthlyBudgets.find(yearMonth);
    return (it != monthlyBudgets.end()) ? it->second : 0.0;
}

double Budget::getCategoryBudget(const std::string& category) const {
    auto it = categoryBudgets.find(category);
    return (it != categoryBudgets.end()) ? it->second : 0.0;
}

bool Budget::removeMonthlyBudget(const std::string& yearMonth) {
    auto it = monthlyBudgets.find(yearMonth);
    if (it != monthlyBudgets.end()) {
        monthlyBudgets.erase(it);
        std::cout << "已删除月度预算：" << yearMonth << std::endl;
        saveToFile();
        return true;
    }
    std::cout << "未找到该月度预算。" << std::endl;
    return false;
}

bool Budget::removeCategoryBudget(const std::string& category) {
    auto it = categoryBudgets.find(category);
    if (it != categoryBudgets.end()) {
        categoryBudgets.erase(it);
        std::cout << "已删除类别预算：" << category << std::endl;
        saveToFile();
        return true;
    }
    std::cout << "未找到该类别预算。" << std::endl;
    return false;
}

void Budget::displayAllBudgets() const {
    std::cout << "\n========== 预算配置 ==========" << std::endl;

    if (!monthlyBudgets.empty()) {
        std::cout << "\n月度预算：" << std::endl;
        for (const auto& pair : monthlyBudgets) {
            std::cout << "  " << pair.first << ": "
                      << std::fixed << std::setprecision(2) << pair.second << std::endl;
        }
    } else {
        std::cout << "\n暂无月度预算设置。" << std::endl;
    }

    if (!categoryBudgets.empty()) {
        std::cout << "\n类别预算：" << std::endl;
        for (const auto& pair : categoryBudgets) {
            std::cout << "  " << pair.first << ": "
                      << std::fixed << std::setprecision(2) << pair.second << std::endl;
        }
    } else {
        std::cout << "\n暂无类别预算设置。" << std::endl;
    }

    std::cout << "================================" << std::endl;
}

bool Budget::checkMonthlyBudgetExceeded(const std::string& yearMonth, double actualExpense) const {
    double budget = getMonthlyBudget(yearMonth);
    if (budget == 0.0) return false;  // 未设置预算
    return actualExpense > budget;
}

bool Budget::checkCategoryBudgetExceeded(const std::string& category, double actualExpense) const {
    double budget = getCategoryBudget(category);
    if (budget == 0.0) return false;  // 未设置预算
    return actualExpense > budget;
}

double Budget::getMonthlyBudgetUsage(const std::string& yearMonth, double actualExpense) const {
    double budget = getMonthlyBudget(yearMonth);
    if (budget == 0.0) return 0.0;
    return (actualExpense / budget) * 100.0;
}

double Budget::getCategoryBudgetUsage(const std::string& category, double actualExpense) const {
    double budget = getCategoryBudget(category);
    if (budget == 0.0) return 0.0;
    return (actualExpense / budget) * 100.0;
}

bool Budget::saveToFile() const {
    std::ofstream file(budgetFile);
    if (!file.is_open()) {
        std::cerr << "无法打开预算文件进行写入。" << std::endl;
        return false;
    }

    // 保存月度预算
    for (const auto& pair : monthlyBudgets) {
        file << "MONTHLY|" << pair.first << "|" << pair.second << std::endl;
    }

    // 保存类别预算
    for (const auto& pair : categoryBudgets) {
        file << "CATEGORY|" << pair.first << "|" << pair.second << std::endl;
    }

    file.close();
    return true;
}

bool Budget::loadFromFile() {
    std::ifstream file(budgetFile);
    if (!file.is_open()) {
        return false;  // 文件不存在，这是正常的
    }

    monthlyBudgets.clear();
    categoryBudgets.clear();
    std::string line;

    while (std::getline(file, line)) {
        std::istringstream iss(line);
        std::string type, key;
        double amount;

        if (std::getline(iss, type, '|') &&
            std::getline(iss, key, '|') &&
            (iss >> amount)) {

            if (type == "MONTHLY") {
                monthlyBudgets[key] = amount;
            } else if (type == "CATEGORY") {
                categoryBudgets[key] = amount;
            }
        }
    }

    file.close();
    return true;
}
